package com.gec.anan.customer.service.impl;

import com.gec.anan.common.constant.RedisConstant;
import com.gec.anan.common.execption.AnanException;
import com.gec.anan.common.result.Result;
import com.gec.anan.common.result.ResultCodeEnum;
import com.gec.anan.customer.client.CustomerInfoFeignClient;
import com.gec.anan.customer.service.CustomerService;
import com.gec.anan.model.form.customer.UpdateWxPhoneForm;
import com.gec.anan.model.vo.customer.CustomerLoginVo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerInfoFeignClient client;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String login(String code) {
        //返回的token信息
        //1、远程调用service-customer服务，获取返回的数据{code:xx,data:{customerId:xx}},
        // data为customerId
        Result<Long> login = client.login(code);
        //处理调用和返回的出错
//        if(login.getCode()!=200){
//            //自定义的异常
//            throw new AnanException(login.getCode(), login.getMessage());
//        }
        //返回的是customerId
        Long customerId = login.getData();
        //处理远程调用的错误
//        if(customerId==null){
//            throw  new AnanException(ResultCodeEnum.DATA_ERROR);
//        }
        //2、生成token
        String token =UUID.randomUUID().toString().replace("-", "");
        //3、将token存入redis中，设置ttl
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token, customerId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
        //4、返回token
        return  token;
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfo(Long customerId) {
        Result<CustomerLoginVo> customerLoginInfo = client.getCustomerLoginInfo(customerId);
        if (customerLoginInfo==null){
            throw new AnanException(customerLoginInfo.getCode(), customerLoginInfo.getMessage());
        }
        CustomerLoginVo customerLoginVo = customerLoginInfo.getData();
        if(customerLoginVo==null){
            throw new AnanException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginVo;
    }

    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        client.updateWxPhoneNumber(updateWxPhoneForm);
        return true;
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfo2(String token) {
        if(StringUtils.isEmpty(token)){
            throw new AnanException(ResultCodeEnum.LOGIN_AUTH  );
        }

        String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        if(StringUtils.isEmpty(customerId)){
            throw new AnanException(ResultCodeEnum.LOGIN_AUTH  );
        }
        Result<CustomerLoginVo> customerLoginInfo = client.getCustomerLoginInfo(Long.parseLong(customerId));
        if(customerLoginInfo.getCode()!=200){
            throw new AnanException(ResultCodeEnum.DATA_ERROR);
        }
        if(customerLoginInfo.getData()==null){
            throw new AnanException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginInfo.getData();
    }
}
